package com.hrcx.mystery.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hrcx.mystery.common.CommonPage;
import com.hrcx.mystery.common.PageParamRequest;
import com.hrcx.mystery.dto.MysteryConsignmentDto;
import com.hrcx.mystery.exception.MysteryException;
import com.hrcx.mystery.pojo.*;
import com.hrcx.mystery.service.EbMysteryMerchandiseAssociatedService;
import com.hrcx.mystery.service.EbMysteryOutOfRecordService;
import com.hrcx.mystery.service.EbMysteryUserConsignmentService;
import com.hrcx.mystery.mapper.EbMysteryUserConsignmentMapper;
import com.hrcx.mystery.service.EbStoreProductService;
import com.hrcx.mystery.vo.EbMysteryUserConsignmentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author LinXiPeng
 */
@Service
@Slf4j
public class EbMysteryUserConsignmentServiceImpl extends ServiceImpl<EbMysteryUserConsignmentMapper, EbMysteryUserConsignment>
        implements EbMysteryUserConsignmentService {
    private final Integer uid = 1;
    /**
     * 注入盲盒商品扩展服务
     */
    @Resource
    private EbMysteryMerchandiseAssociatedService associatedService;

    /**
     * 注入开箱记录服务
     */
    @Resource
    private EbMysteryOutOfRecordService recordService;

    /**
     * 注入原始商品服务
     */
    @Resource
    private EbStoreProductService ebStoreProductService;

    /**
     * 注入数据库交互层
     */
    @Resource
    private EbMysteryUserConsignmentMapper consignmentMapper;


    @Override
    public Boolean compoundPrize(Integer rid, BigDecimal price) {
        EbMysteryOutOfRecord record = recordService.getById(rid);
        if (record != null && record.getUid().equals(uid) && record.getStatus() == 0) {
            //获得奖品扩展对象
            EbMysteryMerchandiseAssociated associated = associatedService.lambdaQuery().eq(EbMysteryMerchandiseAssociated::getId, record.getEpid())
                    .select(EbMysteryMerchandiseAssociated::getPid, EbMysteryMerchandiseAssociated::getConsignment, EbMysteryMerchandiseAssociated::getType)
                    .one();
            if (!associated.getType()) {
                throw new MysteryException("抱歉,虚拟奖品不可寄售!");
            }
            log.info("记录对象" + record);
            //获得商品价格
            EbStoreProduct product = ebStoreProductService.lambdaQuery().eq(EbStoreProduct::getId, associated.getPid()).select(EbStoreProduct::getPrice).one();
            BigDecimal canConsignmentPrice = product.getPrice().multiply(BigDecimal.valueOf(associated.getConsignment()));
            if (canConsignmentPrice.compareTo(price) < 0) {
                throw new MysteryException("寄售价格已超过系统限制!");
            }
            EbMysteryUserConsignment ebMysteryUserConsignment = new EbMysteryUserConsignment();
            ebMysteryUserConsignment.setRid(rid);
            ebMysteryUserConsignment.setCreatTime(new Date());
            ebMysteryUserConsignment.setStatus(0);
            ebMysteryUserConsignment.setPrice(price);
            this.save(ebMysteryUserConsignment);
            record.setStatus(5);
            //更新记录状态
            return recordService.updateById(record);
        }
        throw new MysteryException("寄售失败,原因可能是:无记录,用户无法对应,奖品已经被提货和奖品类型为虚拟等...");
    }

    @Override
    public Boolean cancelCompoundPrize(Integer cid) {
        EbMysteryUserConsignment consignment = this.getById(cid);
        //更新盲盒寄售的状态
        if (consignment.getStatus() == 0) {
            consignment.setStatus(1);
            this.updateById(consignment);
            //更新记录中的状态
            EbMysteryOutOfRecord record = new EbMysteryOutOfRecord();
            record.setId(consignment.getRid());
            record.setStatus(0);
            return recordService.updateById(record);
        }
        return false;
    }

    @Override
    public PageInfo<EbMysteryUserConsignmentVo> getCompoundPrize(MysteryConsignmentDto consignmentDto, PageParamRequest pageParamRequest) {
        Page<EbMysteryUserConsignment> mysteryUserConsignments = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        List<EbMysteryUserConsignmentVo> compoundPrize = consignmentMapper.getCompoundPrize(consignmentDto, uid);
        return CommonPage.copyPageInfo(mysteryUserConsignments, compoundPrize);
    }
}




